package com.ricky.onlineShop.read.trading.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ricky.admin.util.LoginUserUtils;
import com.ricky.common.core.domain.model.LoginUser;
import com.ricky.common.utils.DateUtils;
import com.ricky.onlineShop.read.trading.mapper.TradingMapper;
import com.ricky.onlineShop.read.trading.service.TradingReadService;
import com.ricky.onlineShop.shop.trading.enums.ReputationLevelEnum;
import com.ricky.onlineShop.shop.trading.mapStruct.TradingStruct;
import com.ricky.onlineShop.shop.trading.pojo.Trading;
import com.ricky.onlineShop.shop.trading.pojo.TradingOrder;
import com.ricky.onlineShop.shop.trading.pojo.TradingPerformance;
import com.ricky.onlineShop.shop.trading.service.ITradingFileService;
import com.ricky.onlineShop.shop.trading.service.ITradingOrderService;
import com.ricky.onlineShop.shop.trading.service.ITradingPerformanceService;
import com.ricky.onlineShop.shop.trading.service.ITradingService;
import com.ricky.onlineShop.shop.trading.vo.TradingVo;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author staticlan
 * @Date 2024/7/4 15:37
 * @Description:
 */

@Service
public class TradingReadServiceImpl implements TradingReadService {
    @Autowired
    private TradingMapper tradingMapper;

    @Autowired
    private ITradingFileService tradingFileService;
    @Autowired
    private ITradingOrderService tradingOrderService;
    @Autowired
    private ITradingService iTradingService;
    @Autowired
    private ITradingPerformanceService iTradingPerformanceService;


    @Value("${ricky.photoPath}")
    private String fileTemplate;

    /**
     * 查询trading
     *
     * @param id trading主键
     * @return trading
     */
    @Override
    public Trading selectTradingById(String id)
    {
        return tradingMapper.selectTradingById(id);
    }

    /**
     * 查询trading列表
     *
     * @param trading trading
     * @return trading
     */
    @Override
    public List<Trading> selectTradingList(Trading trading) {
        List<Trading> tradings = tradingMapper.selectTradingList(trading);
        for (Trading trading1 : tradings) {
            if (ObjectUtil.isNotEmpty(trading1)){
                String reputationLevel = trading1.getReputationLevel();
                ReputationLevelEnum byKey = ReputationLevelEnum.getByKey(reputationLevel);
                if (byKey != null) {
                    String fileUrl = fileTemplate + "/profile/template/" + byKey.getDesc();
                    trading1.setReputationLevel(fileUrl);
                }else {
                    trading1.setReputationLevel("");
                }
            }
        }
        return tradings;
    }

    @Override
    public List<Trading> selectMyTradingList(Trading trading) {
        Long userId = LoginUserUtils.getUser().getUserId();
        trading.setShopHolderId(userId.toString());
        List<Trading> tradings = tradingMapper.selectTradingList(trading);
        for (Trading trading1 : tradings) {
            if (ObjectUtil.isNotEmpty(trading1)){
                String reputationLevel = trading1.getReputationLevel();
                ReputationLevelEnum byKey = ReputationLevelEnum.getByKey(reputationLevel);
                if (byKey != null) {
                    String fileUrl = fileTemplate + "/profile/template/" + byKey.getDesc();
                    trading1.setReputationLevel(fileUrl);
                }else {
                    trading1.setReputationLevel("");
                }
            }
        }
        return tradings;
    }

    /**
     * 新增trading
     *
     * @param trading trading
     * @return 结果
     */
    @Override
    public int insertTrading(Trading trading)
    {
        trading.setCreateTime(DateUtils.getNowDate());
        return tradingMapper.insertTrading(trading);
    }

    /**
     * 修改trading
     *
     * @param trading trading
     * @return 结果
     */
    @Override
    public int updateTrading(Trading trading)
    {
        trading.setUpdateTime(DateUtils.getNowDate());
        return tradingMapper.updateTrading(trading);
    }

    /**
     * 批量删除trading
     *
     * @param ids 需要删除的trading主键
     * @return 结果
     */
    @Override
    public int deleteTradingByIds(String[] ids) {
        try {
            if (ObjectUtil.isNotEmpty(ids)){
                for (String id : ids){
                    if (ObjectUtil.isNotEmpty(id)){
                        tradingFileService.deleteAllByMainId(id);
                    }
                    TradingOrder tradingOrder1 = tradingOrderService.findTradingOrderByTradingId(id);
                    if (ObjectUtil.isNotEmpty(tradingOrder1)){
                        //tradingOrderService.deleteTradingOrderById(tradingOrder.getId());

                        // 查询删除对象
                        TradingOrder tradingOrder = tradingOrderService.getById(tradingOrder1.getId());
                        if (tradingOrder == null) {
                            throw new RuntimeException("未找到订单信息信息！");
                        }
                        String tradingId = tradingOrder.getTradingId();
                        try {
                            // 查询订单对应绩效
                            List<TradingPerformance> byTradingId = iTradingPerformanceService.findByTradingId(tradingId);
                            // 删除订单对应绩效
                            for (TradingPerformance tradingPerformance : byTradingId) {
                                iTradingPerformanceService.delete(tradingPerformance);
                            }
                            // 删除订单
                            tradingOrderService.deleteById(tradingOrder.getId());
                        } catch (Exception e) {
                            throw new RuntimeException("订单信息删除失败！");
                        }
                    }
                }
            }
            return tradingMapper.deleteTradingByIds(ids);
        }catch (Exception e){
            throw new RuntimeException("删除失败!");
        }

    }

    /**
     * 删除trading信息
     *
     * @param id trading主键
     * @return 结果
     */
    @Override
    public int deleteTradingById(String id)
    {
        return tradingMapper.deleteTradingById(id);
    }

    @Override
    public int myTradings() {
        String userId = LoginUserUtils.getUserId();
        int nums = 0;
        if (StrUtil.isNotEmpty(userId)) {
            Trading trading = new Trading();
            trading.setShopHolderId(userId);
            nums = tradingMapper.countTradingList(trading);
        }
        return nums;
    }
}
